<?php
include "../site_specific/project.php";
include "../includes/constants.php";
include "../site_specific/external_constants.php";
include "../includes/phpmailer/class.phpmailer.php";
include "../includes/phpmailer/class.smtp.php";
include "../includes/utilities.php";
include "../includes/pages.php";
include "../includes/database.php";
include "../includes/users.php";
include "../includes/date.php";
include "../includes/text.php";
include "../includes/files.php";

# Establish database connection
establishDatabaseConnection();

$get_text = $_GET["text"];

$get_id = $_GET["id"];

$return_content = ""; 

if($get_text!=""){
	
	$sql_insert_tags = "Insert into ".$PROJECT_DB_TABLES["tags"]." (t_tag) values ('".$get_text."')";
	
	try{
		executeSQLQuery($sql_insert_tags);
	}
	catch(Exception $e){
		
		echo "Insert is not sucessful";
	}
	
	
	$sql_tags = "SELECT DISTINCT t_tag FROM ".$PROJECT_DB_TABLES["tags"]." order by t_tag";
						  
	$sql_selected_tags = "SELECT DISTINCT t_tag FROM ".$PROJECT_DB_TABLES["blogtag"]." bt JOIN ".$PROJECT_DB_TABLES["blogs"]." b ON b.b_index = bt.bt_blog JOIN ".$PROJECT_DB_TABLES["tags"]." t ON bt.bt_tag = t.t_index WHERE b_id=\"".$get_id."\"";
						  
	#Get the data result
	$data_tags = getRecord($sql_tags);
	  
	$data_selected_tags = getRecord($sql_selected_tags);
	  
	$return_content .="<div id=\"div_selected_tags\">";
									  
	foreach($data_tags as $tag_keys =>$tag_values){
			  
		if(in_array($tag_values, $data_selected_tags)){
			
			$return_content .= "&nbsp;&nbsp;".$tag_values["t_tag"].":<input type=\"checkbox\" id=\"".$tag_values["t_tag"]."\" name=\"".$tag_values["t_tag"]."\" value=\"".$tag_values["t_tag"]."\" onchange=\"saveTags('".$tag_values["t_tag"]."','".$get_id."')\" checked=\"checked\" />";
		}
		else{
			
			$return_content .= "&nbsp;&nbsp;".$tag_values["t_tag"].":<input type=\"checkbox\" id=\"".$tag_values["t_tag"]."\" name=\"".$tag_values["t_tag"]."\" value=\"".$tag_values["t_tag"]."\" onchange=\"saveTags('".$tag_values["t_tag"]."','".$get_id."')\" />";
		}
	} 
	
	$return_content .="</div>";
	
	$return_content .= "%%%###";
	
	$count = 0; 
	
	foreach($data_tags as $data_tags_values){
		
		if($count == 0){	  
			$return_content.= $data_tags_values["t_tag"];
		}
		else{
			$return_content.= "@@@!!!".$data_tags_values["t_tag"];
		}
		$count ++;
	}
	
	echo $return_content;
					  
}
?>